package com.ifacebox.arcsoft.face;

import com.arcsoft.face.EngineConfiguration;
import com.arcsoft.face.FunctionConfiguration;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author znn
 */
@Configuration
public class ArcSoftFaceConfig {
    @Resource
    private ArcSoftFaceConfigProperties faceConfigProperties;

    @Bean
    public FunctionConfiguration functionConfiguration() {
        FunctionConfiguration functionConfiguration = new FunctionConfiguration();
        functionConfiguration.setSupportFaceDetect(true);
        functionConfiguration.setSupportFaceRecognition(true);
        functionConfiguration.setSupportAge(false);
        functionConfiguration.setSupportGender(false);
        functionConfiguration.setSupportFace3dAngle(false);
        functionConfiguration.setSupportLiveness(faceConfigProperties.isSupportLiveness());
        functionConfiguration.setSupportIRLiveness(false);
        return functionConfiguration;
    }

    @Bean
    public EngineConfiguration engineConfiguration(FunctionConfiguration functionConfiguration) {
        EngineConfiguration engineConfiguration = new EngineConfiguration();
        engineConfiguration.setFunctionConfiguration(functionConfiguration);
        engineConfiguration.setDetectFaceMaxNum(10);
        engineConfiguration.setDetectFaceScaleVal(30);
        engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
        engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);
        return engineConfiguration;
    }

    @Bean
    public ArcSoftFaceEnginePool faceEnginePool(EngineConfiguration engineConfiguration
            , GenericObjectPoolConfig<ArcSoftFaceEngine> objectPoolConfig) throws Exception {
        ArcSoftFaceEnginePool faceEnginePool = new ArcSoftFaceEnginePool(faceConfigProperties.getLibPath()
                , faceConfigProperties.getAppId(), faceConfigProperties.getSdkKey()
                , engineConfiguration, objectPoolConfig);
        faceEnginePool.addObjects(faceConfigProperties.getFaceMaxThreads());
        return faceEnginePool;
    }

    @Bean
    public GenericObjectPoolConfig<ArcSoftFaceEngine> objectPoolConfig() {
        GenericObjectPoolConfig<ArcSoftFaceEngine> objectPoolConfig = new GenericObjectPoolConfig<>();
        objectPoolConfig.setMaxIdle(faceConfigProperties.getFaceMaxThreads());
        objectPoolConfig.setMinIdle(faceConfigProperties.getFaceMaxThreads());
        objectPoolConfig.setMaxTotal(faceConfigProperties.getFaceMaxThreads());
        objectPoolConfig.setMaxWait(faceConfigProperties.getFaceMaxWait());
        objectPoolConfig.setJmxEnabled(false);
        return objectPoolConfig;
    }
}
